<template>
	<view class="vwh" style="overflow: hidden;">
		<view class="my_tea wh100 df-yc">
			<!-- 导航栏 -->
			<view class="head_box w100" :style="{ background: bgcolor}">
				<cu-custom :icon="true" :isBack="false">
					<block slot="backText"></block>
				</cu-custom>
			</view>
			<view class="wh100 my_tea_content bs">
				<view class="scroll_wrap h100 br-20">
					<scroll-view @scrolltolower="loadMore" :scroll-y="true">
						<view class="df-yc ds">
							<view class="mt20">
								<ad></ad>
							</view>
							<view class="w100" style="padding-bottom: 300rpx;">
								<view id='tea_lists' class="tea_lists df-yc ai bs jc mt30 px20">
									<view v-if="teaList.length==0&&isLogin">
										<shopro-empty :isFixed="false" :emptyData="emptyData"></shopro-empty>
									</view>
									<view v-if="!isLogin">
										<u-empty icon-size="60rpx" text="未登录,暂无法查看" mode="permission">
											<template slot="bottom">
												<u-button @click="login" size="medium" type="default" :ripple="true">去登录
												</u-button>
											</template>
										</u-empty>
									</view>
									<view @click="jump('/pages/goods/box-detail/box-detail',{teabox_id:item.teabox_id})"
										class="tea_item w100 df py30" v-for="item in teaList" :key="item.code">
										<view style="width: 218rpx;height: 218rpx;" class="img">
											<image style="width: 218rpx;height: 218rpx;" :src="imgUrl+item.image"
												mode="widthFix"></image>
										</view>
										<view class="right w100 df-yb bs">
											<view class="tea_name fb fs30">{{item.name}}</view>
											<view class="tea_id fs24">唯一标识：{{item.code}}</view>
											<view class="bottom w100 df">
												<view class="size w100 df">
													<view class="df-c fs22">
														x{{item.count_num}}/罐
													</view>
													<view class="df-c fs22">
														{{item.weight/item.count_num}}g/罐
													</view>
												</view>
												<view class="progress df ai fs22">
													{{item.open_num==item.count_num?'点亮完成':'点亮进度'}}：{{item.open_num}}/{{item.count_num}}
												</view>
											</view>
										</view>
									</view>
									<u-loading :show="loading" mode="circle"></u-loading>
								</view>
							</view>
						</view>
					</scroll-view>
				</view>
				<view class="tabbar">
					<view class="scan" @tap="openScan">
						<image class="image" :src="temImgUrl+'/scan.png'" mode="widthFix"></image>
						扫码添加成事茶
					</view>
				</view>
			</view>
		</view>
		<shopro-tabbar></shopro-tabbar>
		<shopro-login-modal></shopro-login-modal>
	</view>
</template>

<script>
	import {
		mapMutations,
		mapActions,
		mapState
	} from 'vuex';
	import ad from "./components/ad.vue"
	export default {
		components: {
			ad
		},
		data() {
			return {
				temImgUrl: this.$IMG_URL_TEMP,
				bgcolor: "RGBA(241, 241, 241, 1)",
				teaList: [],
				emptyData: {
					tip: "您还未添加茶盒"
				},
				imgUrl: this.$IMG_URL,
				code: "", //判断是否从外部进入
				loading: false,
				currentPage: 1,
				totalPage: 1
			}
		},
		computed: {
			...mapState({
				customBar: state => state.init.customBar,
			}),
			isLogin() {
				return Boolean(uni.getStorageSync('token'))
			},
		},
		created() {
			if (this.isLogin) {
				this.teaList = []
				this.getMyTea()
			}
		},
		onLoad(options) {
			if (options && options.code) {
				this.code = options.code
				this.scan(options.code)
			} else if (this.$Route.query.code) {
				this.code = this.$Route.query.code
				this.scan(this.$Route.query.code)
			}
		},
		onPullDownRefresh() {
			this.teaList = []
			this.currentPage = 1
			this.getMyTea()
		},
		methods: {
			jump(path, params) {
				this.$Router.push({
					path: path,
					params: params
				})
			},
			loadMore() {
				if (this.currentPage < this.totalPage) {
					this.loading = true
					this.currentPage++
					this.getMyTea()
				}
			},
			gotoPage(url) {
				this.$tools.routerTo(url)
			},
			login() {
				this.$store.commit('LOGIN_TIP', true);
			},
			openScan() {
				let that = this
				uni.scanCode({
					success: function(res) {
						that.scan(that.codeParse(res.result))
					}
				});
			},
			// url解析code
			codeParse(url) {
				let codeStr = url.split('?')[1]
				let code = codeStr.split('=')[1]
				return code
			},
			// 获取我的成事茶
			getMyTea() {
				const that = this
				const token = uni.getStorageSync('token')
				this.$api('tea.openBoxHistory', {
					token,
					page: this.currentPage,
					limit: 5
				}).then(res => {
					if (res.code == 1) {
						that.teaList = that.teaList.concat(res.data.data)
						this.totalPage = res.data.last_page
						uni.stopPullDownRefresh()
						that.loading = false
					}
				}).catch(err => {
					if (err.data.code == 404) {
						that.teaList = []
						uni.stopPullDownRefresh()
					}
				})
			},
			scan(code) {
				uni.showLoading({
					title: '添加中',
					icon: 'loading'
				})
				const that = this
				this.$api('tea.scanAdd', {
					code
				}).then(res => {
					if (res.code == 401)
						wx.showModal({
							title: '添加失败',
							content: res.msg,
							success: function(res) {
								if (res.confirm) {
									this.$store.commit('LOGIN_TIP', true)
								} else {
									console.log('用户点击取消')
								}

							}
						})
					if (res.code == 1) {
						uni.hideLoading()
						uni.showLoading({
							title: '添加成功',
							icon: 'success'
						})
						setTimeout(function() {
							that.jump("/pages/scan/scanSuccess/scanSuccess", {
								data: JSON.stringify(res.data),
								type: res.data.pro_type
							})
							uni.hideLoading()
						}, 1000);
					} else if (res.code == 0) {
						setTimeout(function() {
							uni.navigateBack()
						}, 1000);
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.my_tea {
		background-color: RGBA(241, 241, 241, 1);

		.my_tea_content {
			height: 100%;
			overflow: hidden;
			padding: 0 30rpx;

			scroll-view {
				width: 100%;
			}
		}

		.tea_lists {
			background-color: #FFFFFF;
			width: 100%;
			min-height: 200rpx;
			border-radius: 20rpx;
			box-shadow: 2rpx 0px 110rpx 0px rgba(153, 153, 153, 0.16);

			.tea_item {
				justify-content: space-between;

				.img {
					margin-right: 14rpx;

				}

				.right {
					flex: 1;
					padding: 20rpx 0;

					.tea_name {
						color: #191919;
					}

					.tea_id {
						color: #A6B2BF;
					}

					.bottom {
						justify-content: space-between;

						.size {
							view {
								padding: 5rpx 18rpx;
								background: #FFD190;
								border-radius: 4rpx;
								color: #742931;
								margin-right: 5rpx;
							}
						}

						.progress {
							flex-shrink: 0;
							color: rgba(117, 42, 51, 1);
							justify-self: flex-end;
						}

					}
				}
			}

			.tea_item {
				border-bottom: 1rpx solid rgba(92, 107, 130, 0.1);
			}

			.tea_item:last-child {
				border-bottom: none;
			}

		}

		.tabbar {
			position: fixed;
			bottom: 140rpx;
			width: 750rpx;
			height: 189rpx;
			border-radius: 64rpx 64rpx 0px 0px;
			display: flex;
			left: 0;
			justify-content: center;

			.scan {
				margin-top: 30rpx;
				width: 354rpx;
				height: 90rpx;
				background: #B83F46;
				border-radius: 45rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				color: #FFFFFF;

				.image {
					width: 26rpx;
					margin-right: 16rpx;
				}
			}
		}
	}
</style>
